﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Configuration;
using System.Diagnostics;

namespace SdmxMl.HelpDoc
{
    /// <summary> Class for Help Doc menu items support </summary>
    public class DocMenuItems
    {
        #region Internal Members

        private ToolStripMenuItem parentmenuItem;

        #endregion

        #region C'tor

        public DocMenuItems(ToolStripMenuItem parent)
        {
            parentmenuItem = parent;
        }
        #endregion

        #region Doc Sub Menus construction

        public void ConstructMenuItems()
        {
            //In case of error, it will creates nothing
            try
            {
                // from parent tage deduce configuration key
                string key = parentmenuItem.Tag.ToString();
                
                string dir = GetDocDirPath(key);

                // For each file make a menu item
                DirectoryInfo di = new DirectoryInfo(dir);
                FileInfo[] rgFiles = di.GetFiles("*.*");
                foreach (FileInfo fi in rgFiles)
                {
                    // Create a menu item from filename without ext
                    ToolStripMenuItem m = new ToolStripMenuItem();

                    m.Text = Path.GetFileNameWithoutExtension(fi.Name);
                    m.Tag = Path.GetExtension(fi.Name);

                    // attach to parent
                    parentmenuItem.DropDownItems.Add(m);

                    // create click event
                    m.Click += new EventHandler(m_Click);

                    // If user manuel doc encountered, aDD parent, parent as st item
                    if (m.Text == "SAE User Guide")
                    {
                        ToolStripMenuItem mUserManual = new ToolStripMenuItem();
                        mUserManual.Text = "User Manual";
                        mUserManual.Tag = fi.FullName;
                        mUserManual.Click += new EventHandler(m_ClickUserManual);
                        mUserManual.Image = SdmxMl.Properties.Resources.Help_File___hlp_;
                        // attach to parent-parent 
                        ToolStrip ts = parentmenuItem.Owner;
                        if (ts != null)
                        {
                            ts.Items.Insert(0, mUserManual);
                            ts.Items.Insert(1, new ToolStripSeparator());
                        }
                    }
                }

            }
            catch
            {
            }
        }

        protected static string GetDocDirPath(string key)
        {                
                // Doc located in sub directory
                string appDir = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

                // Subdir in config file
                string subDir = ConfigurationManager.AppSettings["Doc"+key].Trim();

                string dir = appDir + "\\" + subDir;

            return dir;
        }
        #endregion

        #region Doc menu Events

        /// <summary> Doc sub menu event click </summary>
        /// <param name="sender"></param> <param name="e"></param>
        protected static void m_Click(object sender, EventArgs e)
        {
            try
            {
                 Process myProc;
                 ToolStripMenuItem m = sender as ToolStripMenuItem;
                 string filePath = GetDocDirPath(m.OwnerItem.Tag.ToString()) + "\\" + m.Text + m.Tag;
                 myProc = Process.Start(filePath);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary> User Manuel event click </summary>
        /// <param name="sender"></param> <param name="e"></param>
        protected static void m_ClickUserManual(object sender, EventArgs e)
        {
            try
            {
                Process myProc;
                ToolStripMenuItem m = sender as ToolStripMenuItem;
                myProc = Process.Start(m.Tag.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion
    }
}
